package com.ruoyi.system.carsale.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.carsale.domain.CarFct;
import com.ruoyi.system.carsale.domain.CarSeries;
import com.ruoyi.system.carsale.domain.CarType;
import com.ruoyi.system.carsale.domain.vo.CarTypeVo;
import com.ruoyi.system.carsale.service.ICarSeriesService;
import com.ruoyi.system.carsale.service.ICarTypeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 车辆款式Controller
 * 
 * @author ruoyi
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/carType")
public class CarTypeController extends BaseController
{
    @Autowired
    private ICarTypeService carTypeService;
    @Autowired
    private ICarSeriesService carSeriesService;
    /**
     * 查询车辆款式列表
     */
    @RequiresPermissions("system:carType:list")
    @GetMapping("/list")
    public TableDataInfo list(CarType carType)
    {
        startPage();
        List<CarType> list = carTypeService.selectCarTypeList(carType);
        if(list.size() > 0) {
            List<Long> seriesIds = list.stream().map(a -> a.getSeriesId()).collect(Collectors.toList());
            CarSeries series = new CarSeries();
            series.setSeriesIds(seriesIds.stream().distinct().collect(Collectors.toList()));
            List<CarSeries> seriesList = carSeriesService.selectCarSeriesList(series);
            Map<Long, CarSeries> seriesMap = seriesList.stream()
                    .collect(Collectors.toMap(CarSeries::getSeriesId, Function.identity(), (k1, k2) -> k1));
            for(CarType c : list) {
                CarSeries series1 =  seriesMap.get(c.getSeriesId());
                if(series1 == null) {
                    continue;
                }
                c.setBrandName(series1.getBrandName());
                c.setFctName(series1.getFctName());
                c.setSeriesName(series1.getSeriesName());
            }
        }
        return getDataTable(list);
    }
    /**
     * 查询车辆款式列表
     */
    @GetMapping("/carList")
    public TableDataInfo carList(CarTypeVo carTypeVo)
    {
        startPage();
        List<CarTypeVo> list = carTypeService.selectCarList(carTypeVo);
        return getDataTable(list);
    }
    /**
     * 导出车辆款式列表
     */
    @RequiresPermissions("system:carType:export")
    @Log(title = "车辆款式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarType carType)
    {
        List<CarType> list = carTypeService.selectCarTypeList(carType);
        ExcelUtil<CarType> util = new ExcelUtil<CarType>(CarType.class);
        util.exportExcel(response, list, "车辆款式数据");
    }

    /**
     * 获取车辆款式详细信息
     */
    @RequiresPermissions("system:carType:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(carTypeService.selectCarTypeById(id));
    }

    /**
     * 新增车辆款式
     */
    @RequiresPermissions("system:carType:add")
    @Log(title = "车辆款式", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarType carType)
    {
        return toAjax(carTypeService.insertCarType(carType));
    }

    /**
     * 修改车辆款式
     */
    @RequiresPermissions("system:carType:edit")
    @Log(title = "车辆款式", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarType carType)
    {
        return toAjax(carTypeService.updateCarType(carType));
    }

    /**
     * 删除车辆款式
     */
    @RequiresPermissions("system:carType:remove")
    @Log(title = "车辆款式", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carTypeService.deleteCarTypeByIds(ids));
    }
}
